package com.leiting.mail.util;

import com.leiting.mail.util.properties.factory.DefaultMailReaderPropertiesFactory;
import com.leiting.mail.util.properties.factory.MailReaderPropertiesFactory;

import javax.mail.*;
import java.util.Properties;


public class FolderBuilder {
    private FolderBuilder(){}
    public static Folder build(String mailType,final String account,final String password,boolean debug) throws MessagingException {
        if (null == mailType || mailType.trim().length() == 0) {
            throw new MessagingException("Unsupported mailType");
        }
        MailReaderPropertiesFactory factory = new DefaultMailReaderPropertiesFactory();
        Properties props = null;
        Folder folder = null;
        if (mailType.equalsIgnoreCase(MailConstant.MAIL_TYPE_QQ)) {
            props = factory.getPop3StoreProperties(MailConstant.QQ_POP_MAIL_SERVER);
            Session session = Session.getInstance(props);
            session.setDebug(debug);
            //session.setDebug(true);

            Store store = session.getStore(MailConstant.PROTOCOL_TYPE_POP3);
            store.connect(MailConstant.QQ_POP_MAIL_SERVER, account, password);
            folder = store.getFolder("inbox");
            folder.open(Folder.READ_WRITE);
        }else if(mailType.equalsIgnoreCase(MailConstant.EXMAIL_TYPE_QQ)){
            props = factory.getPop3StoreProperties(MailConstant.QQ_POP_EXMAIL_SERVER);
            Session session = Session.getInstance(props,new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    PasswordAuthentication pa = new PasswordAuthentication(account, password);
                    return pa;
                }
            });
            session.setDebug(debug);
            Store store = session.getStore();
            store.connect();
            folder = store.getFolder("inbox");
            folder.open(Folder.READ_ONLY);
        }else if (mailType.equalsIgnoreCase(MailConstant.MAIL_TYPE_163)){
            props = factory.getPop3StoreProperties(MailConstant.WANGYI_POP_MAIL_SERVER);
            Session session = Session.getInstance(props,new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    PasswordAuthentication pa = new PasswordAuthentication(account, password);
                    return pa;
                }
            });
            session.setDebug(debug);
            Store store = session.getStore();
            store.connect();
            folder = store.getFolder("inbox");
            folder.open(Folder.READ_ONLY);
        }else if(mailType.equalsIgnoreCase(MailConstant.MAIL_TYPE_ALIYUN)){
            props = factory.getPop3StoreProperties(MailConstant.ALIYUN_POP_MAIL_SERVER);
            Session session = Session.getInstance(props);
            session.setDebug(debug);
            //session.setDebug(true);

            Store store = session.getStore(MailConstant.PROTOCOL_TYPE_POP3);
            store.connect(MailConstant.ALIYUN_POP_MAIL_SERVER, account, password);
            folder = store.getFolder("inbox");
            folder.open(Folder.READ_WRITE);
        }
        return folder;
    }
}
